package com.sina.shop.eureka.consume.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sina.shop.domain.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 调用服务，并配置断路器，当调用出错时调用默认的配置。
 * 段浩杰   2018-01-31-17:40
 */
@Service("logService")
public class LogService {

    @Autowired
    private RestTemplate restTemplate;

    /*HystrixCommand注解中 如果调用出现错误就会访问 getLogError方法 */
    @HystrixCommand(fallbackMethod = "getLogError")
    public Log getLog(Long id) {
        return restTemplate.getForEntity("http://SHOP-SPRING-CLOUD-LOG/log/getLog?id={id}", Log.class, id).getBody();
    }

    @HystrixCommand(fallbackMethod = "getLogListError")
    public List<Log> getLogList(Log log) {
        return restTemplate.getForEntity("http://SHOP-SPRING-CLOUD-LOG/log/getLogList?log={log}", List.class, log).getBody();
    }

    @HystrixCommand(fallbackMethod = "saveLogError")
    public Log saveLog(Log log) {
        return restTemplate.postForEntity("http://SHOP-SPRING-CLOUD-LOG/log/saveLog?log={log}", log, Log.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "deleteLogError")
    public Integer deleteLog(Long id) {
        return restTemplate.postForEntity("http://SHOP-SPRING-CLOUD-LOG/log/deleteLog?id={id}", id, Integer.class).getBody();
    }

    public Log getLogError(Long id) {
        return null;
    }

    public List<Log> getLogListError(Log log) {
        return null;
    }

    public Log saveLogError(Log log) {
        return null;
    }

    public Integer deleteLogError(Long id) {
        return null;
    }


}
